import java.io.File;
import java.net.URL;
import java.security.ProtectionDomain;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.ThreadPool;
import org.eclipse.jetty.webapp.WebAppContext;

public class Runner {
	private static final String DEFAULT_APP_CONTEXT_PATH = "src/main/webapp";

	public static void main(String[] args) {
		String default_env = "dev";
		int default_port = 8080;
		String default_AppName = "demo-project-web";
		if(args != null && args.length>=3) {
			try {
				default_port = Integer.parseInt(args[0]);
			} catch (Exception e) {
				System.out.println("##########端口转化失败使用默认端口8080,_port=" + args[0]);
			}
			default_AppName = args[1];
			default_env = args[2];
		}
		String conetext_path = "/" + default_AppName;
		runJettyServer(default_port, conetext_path, default_env);
	}

	public static void runJettyServer(int port, String contextPath, String env) {
		
		Server server = initContainerServer();
		
		if("dev".equals(env)) {
			server = createDevServer(port, contextPath, server);
		} else {
			server = createJettyServer(port, contextPath, server);
		}
		System.out.println("##########jettyVersion=" + server.getVersion());
		System.out.println("##########port=" + port);
		System.out.println("##########contextPath=" + contextPath);
		System.out.println("##########env=" + env);
		try {
			server.start();
			server.join();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				server.stop();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static Server createJettyServer(int port, String contextPath, Server server) {
		ServerConnector connector = new ServerConnector(server);
        connector.setPort(port);
        server.setConnectors(new Connector[]{connector});
		
		ProtectionDomain protectionDomain = Runner.class.getProtectionDomain();
		URL location = protectionDomain.getCodeSource().getLocation();
		String warFile = location.toExternalForm();
		WebAppContext context = new WebAppContext(warFile, contextPath);
		context.setServer(server);
		// 设置work dir,war包将解压到该目录，jsp编译后的文件也将放入其中。
		String currentDir = new File(location.getPath()).getParent();
		File workDir = new File(currentDir, "work");
		context.setTempDirectory(workDir);
		server.setHandler(context);
		System.out.println("##########warFile=" + warFile);
		System.out.println("##########currentDir=" + currentDir);
		System.out.println("##########workDir=" + workDir);
		return server;
	}

	public static Server createDevServer(int port, String contextPath, Server server) {
		ServerConnector connector = new ServerConnector(server);
		// 设置服务端口
		connector.setPort(port);
		connector.setReuseAddress(false);
		server.setConnectors(new Connector[] { connector });
		// 设置web资源根路径以及访问web的根路径
		WebAppContext webAppCtx = new WebAppContext(DEFAULT_APP_CONTEXT_PATH, contextPath);
		webAppCtx.setDescriptor(DEFAULT_APP_CONTEXT_PATH + "/WEB-INF/web.xml");
		webAppCtx.setResourceBase(DEFAULT_APP_CONTEXT_PATH);
		webAppCtx.setClassLoader(Thread.currentThread().getContextClassLoader());
		server.setHandler(webAppCtx);
		return server;
	}
	
	public static Server initContainerServer() {
		int pool_size = 200;
		ThreadPool pool = null;
		String _pool_size = System.getenv("REQUEST_POOL_SIZE");
		if(_pool_size!=null) {
			try {
				pool_size = Integer.parseInt(_pool_size);
			} catch (Exception e) {
				System.out.println("##########线程数转化失败使用默认值200,_pool_size=" + _pool_size);
			}
		}
		pool = new QueuedThreadPool(pool_size);
		Server server = new Server(pool);
		server.setStopAtShutdown(true);
		return server;
	}
}